package com.autocode.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.autocode.generator.lib.EnhanceFreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

/**
 * @author haotchen
 * @time 2023/2/13-16:37
 */
public class CodeGenerator {
    public static void main(String[] args) {

        String databaseUrl = "jdbc:mysql://haotchen.top:36000/boger_ci";
        String username = "root";
        String password = "a@199862";
        String tablePrefix = "";

        /**
         * 表格名称列表
         */
        String[] tableList = new String[]{
                "menu"
        };
        /**
         * 生成代码的目标目录,从 com 开始
         */
        String generatorPath = "com.autocode.generator.serve";

        // 数据源配置
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig
                .Builder(
                databaseUrl,
                username,
                password);//内部会自行判断数据库类型

        // 全局配置
        GlobalConfig.Builder globalConfigBuilder = new GlobalConfig.Builder();
        // 代码生成目录
        String projectPath = System.getProperty("user.dir");
        globalConfigBuilder.outputDir(projectPath + "/generator/src/main/java");
        // 开启Swagger配置
        globalConfigBuilder.enableSwagger();
        // 时间类型
        globalConfigBuilder.dateType(DateType.ONLY_DATE);
        // 关闭默认打开目录页面
        globalConfigBuilder.disableOpenDir();

        // 作者
        globalConfigBuilder.author("Haotchen");
        // 是否覆盖旧的文件
        globalConfigBuilder.fileOverride();
        // 实体属性Swagger2注解
        //globalConfigBuilder.enableSwagger();
        // 包配置，如模块名、实体、mapper、service、controller等
        PackageConfig.Builder packageConfigBuilder = new PackageConfig.Builder();
        packageConfigBuilder.parent(generatorPath);
        packageConfigBuilder.entity("model.entity");
        packageConfigBuilder.other("model.parma");
        packageConfigBuilder.mapper("mapper");
        packageConfigBuilder.controller("controller");
        packageConfigBuilder.service("service");
        packageConfigBuilder.serviceImpl("service.serviceImpl");

        // 策略配置
        StrategyConfig.Builder strategyConfigBuilder = new StrategyConfig.Builder();
        // 设置需要映射的表名
        strategyConfigBuilder.addInclude(tableList);//字符串数组，可以生成多个表

        // 下划线转驼峰
        strategyConfigBuilder.entityBuilder().naming(NamingStrategy.underline_to_camel);
        strategyConfigBuilder.entityBuilder().columnNaming(NamingStrategy.underline_to_camel);
        // 去除前缀"t_"
        strategyConfigBuilder.addTablePrefix(tablePrefix);

        // entity的Lombok
        strategyConfigBuilder.entityBuilder().enableLombok();
        // 开启链式编程
        strategyConfigBuilder.entityBuilder().enableChainModel();
        // 启用字段注释
        strategyConfigBuilder.entityBuilder().enableLombok().enableTableFieldAnnotation();

        // 待定功能
        strategyConfigBuilder.enableCapitalMode();

        // 逻辑删除
        strategyConfigBuilder.entityBuilder().logicDeleteColumnName("deleted");
        strategyConfigBuilder.entityBuilder().logicDeletePropertyName("deleted");

        // 创建时间
        IFill gmtCreate = new Column("create_time", FieldFill.INSERT);
        // 更新时间
        IFill gmtModified = new Column("update_time", FieldFill.INSERT_UPDATE);
        strategyConfigBuilder.entityBuilder().addTableFills(gmtCreate, gmtModified);

        // 乐观锁
//        strategyConfigBuilder.entityBuilder().enableSerialVersionUID();
//        strategyConfigBuilder.entityBuilder().versionColumnName("version");
//        strategyConfigBuilder.entityBuilder().versionPropertyName("version");

        // 使用Restful风格的Controller
        strategyConfigBuilder.controllerBuilder().enableRestStyle();

        // 将请求地址转换为驼峰命名，如 http://localhost:8080/hello_id_2
        strategyConfigBuilder.controllerBuilder().enableHyphenStyle();

        Map<String, String> customFile = new HashMap<>();
        // DTO 下面的key会作为类名后缀，进而生成新类
        customFile.put("DTO.java", "templates/other/dto.java.ftl");
        customFile.put("VO.java", "templates/other/vo.java.ftl");
//        customFile.put("Controller.java", "templates/other/Controller.java.ftl");
//        customFile.put("Convertor.java", "templates/other/convertor.java.ftl");

        InjectionConfig.Builder injectionConfigBuilder = new InjectionConfig.Builder().customFile(customFile);

        // 创建代码生成器对象，加载配置
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfigBuilder.build());
        autoGenerator.global(globalConfigBuilder.build());
        autoGenerator.packageInfo(packageConfigBuilder.build());
        autoGenerator.strategy(strategyConfigBuilder.build());
        autoGenerator.injection(injectionConfigBuilder.build());
        // 执行
        autoGenerator.execute(new EnhanceFreemarkerTemplateEngine());
    }
}
